/*
 * Created on Nov 21, 2004 by Ming Chow
 *
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import josx.rcxcomm.RCXBean;

public class MotorControl2Client extends JFrame
{
	private JButton motorActionButton;
	private JButton motorMotionButton;
	private static final String START_LABEL="Start Motor";
	private static final String STOP_LABEL="Stop Motor";
	private static final String FORWARD_LABEL="Rotate Forward";
	private static final String BACKWARD_LABEL="Rotate Backward";
	private static int actionState;
	private static int motionState;
	
	public static void main(String[] args)
	{
		actionState=0;
		motionState=0;
		new MotorControl2Client();
	}
	
	public MotorControl2Client()
	{
	    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    Container content = getContentPane();
	    content.setBackground(Color.white);
	    content.setLayout(new FlowLayout());
	    motorActionButton=new JButton(START_LABEL);
	    ActionListener motorActionButtonListener=new
			ActionListener()
			{
	    		public void actionPerformed (ActionEvent e)
	    		{
	    	      try
			      {
	    	      	RCXBean rcxb=new RCXBean();
	    	      	rcxb.setComPort("COM1");
	    			if (actionState==0)
	    			{
	    				motorActionButton.setLabel(STOP_LABEL);
	    				actionState=1;
	    				rcxb.sendInt(8);
	    			}
	    			else
	    			{
	    				motorActionButton.setLabel(START_LABEL);
	    				actionState=0;
	    				rcxb.sendInt(0);
	    			}
	    			rcxb.close();
			      }
	    	      catch (IOException ioe) {};
	    		}
			};
		motorActionButton.addActionListener(motorActionButtonListener);
	    content.add(motorActionButton);
	    
	    motorMotionButton=new JButton(BACKWARD_LABEL);
	    ActionListener motorMotionButtonListener=new
			ActionListener()
			{
	    		public void actionPerformed (ActionEvent e)
	    		{
	    	      try
			      {
	    	      	RCXBean rcxb=new RCXBean();
	    	      	rcxb.setComPort("COM1");
	    			if (motionState==0)
	    			{
	    				motorActionButton.setLabel(FORWARD_LABEL);
	    				motionState=1;
	    				rcxb.sendInt(9);
	    			}
	    			else
	    			{
	    				motorActionButton.setLabel(BACKWARD_LABEL);
	    				actionState=0;
	    				rcxb.sendInt(0);
	    			}
	    			rcxb.close();
			      }
	    	      catch (IOException ioe) {};
	    		}
			};
		motorActionButton.addActionListener(motorActionButtonListener);
	    content.add(motorActionButton);
	    pack();
	    setVisible(true);
	}
}
